var display = [];
var trueDisplay = "0";
var count = 0;
var firstValue = "0";
var operation = "";

var Zheng_OR_Fu=false;//用于表示正负，false表示正，true表示负。
var over_OR_not=false;//计算是否结束的信号量



function appendNumber(number) {
    if (over_OR_not == true){
        return;
    }

    if (trueDisplay != "0"){//如果不是0，则继续添加数字
        display[count] = number;
        count++;
        trueDisplay = display.join("") ;
        if (Zheng_OR_Fu == true) {
            trueDisplay = "-" + trueDisplay;
            updateDisplay();
        }
        updateDisplay();
    }

    if (trueDisplay == "0" ||trueDisplay == 0){//如果是0，则不触发部分按钮
        if (number == "0" || number == "%" || number == "00"){
            return;
        }

        if(number == "."){
            display[0] = '0';
            display[1] = '.';
            count=count+2;
            trueDisplay = '0.' ;
            if (Zheng_OR_Fu == true) {
               trueDisplay = "-" + trueDisplay;
              updateDisplay();
             }
        updateDisplay();
        }

        else{
        display[count] = number;
        count++;
        trueDisplay = display.join("") ;
        if (Zheng_OR_Fu == true) {
            trueDisplay = "-" + trueDisplay;
            updateDisplay();
        }
        updateDisplay();
        }
    }
}

function setOperation(operation) {
    if (over_OR_not == true){
        return;
    }
    switch (operation) {
        case "%":
            if (eval(trueDisplay) != "0") {
                trueDisplay = trueDisplay / 100;
            updateDisplay();
            }
            break;

        case "*":
            upToDisplay2("*");
            break;

        case "/":
            upToDisplay2("/");
            break;

        case "-":
            upToDisplay2("-");
            break;

        case "+":
            upToDisplay2("+");
            break;

    }
}

function clearDisplay() {
    if (trueDisplay != "0") {
        display = [];
        trueDisplay = "0";
        count = "0";
        Zheng_OR_Fu = false;
        updateDisplay();
        updateColor();
    }else{
        display = [];
        trueDisplay = "0";
        count = "0";
        firstValue = "0";
        operation = "";
        Zheng_OR_Fu = false;
        over_OR_not=false;
        firstValue = "0";
        updateDisplay();
        document.getElementById("display_s").innerHTML = "0";
        updateColor();
    }
    if (over_OR_not == true){
        display = [];
        trueDisplay = "0";
        count = "0";
        operation = "";
        Zheng_OR_Fu = false;
        over_OR_not=false;
        firstValue = "0";
        updateDisplay();
        document.getElementById("display_s").innerHTML = "0";
        updateColor();
    }
    
}

function calculateResult() {
    if (operation == "") {
        return;
    }
    if (over_OR_not == true){
        return;
    }

    var result = firstValue + operation + eval(trueDisplay);
    document.getElementById("display_s").innerHTML = result;

    switch (result) {
        case "0*0":
            trueDisplay = "0";
            break;

        case "0/0":
            trueDisplay = "0";
            break;

        default:
            trueDisplay = eval(result).toFixed(10);
            trueDisplay = eval(trueDisplay);
            break;
    }
    updateDisplay();
    over_OR_not=true;
    updateColor();
}

function toggleSign() {
    if (over_OR_not == true){
        return;
    }

    if (Zheng_OR_Fu == false) {
        if (trueDisplay != "0") {
            Zheng_OR_Fu = true;
        trueDisplay = "-" + trueDisplay;
        updateDisplay();
        }   
    } else {
        Zheng_OR_Fu = false;
        trueDisplay = trueDisplay.substring(1);
        updateDisplay();
    }
}

function appendDecimal() {
    if (trueDisplay.includes(".")) {
        return;
    }else{
        appendNumber(".");
    }
}

function updateDisplay() {
    document.getElementById("display").innerHTML = trueDisplay;
}

// 添加键盘事件监听
document.addEventListener('keydown', (event) => {
    if (event.key === 'Backspace') { // 或 event.keyCode === 8 (兼容旧浏览器)
        event.preventDefault(); // 阻止默认退格行为
        backspace();
    }
});

function backspace() {
    if (over_OR_not == true){
        return;
    }
    if (trueDisplay == "0"|| trueDisplay == 0) {
        return;
    }

    if (trueDisplay != "0") {
        display.pop();
        count--;
        trueDisplay = display.join("");
        updateDisplay();
    }
}

function upToDisplay2(choose) {
    firstValue = eval(trueDisplay);
    operation = choose;
    document.getElementById("display_s").innerHTML = firstValue + operation;
    clearDisplay();
}



function updateColor(){
        if(over_OR_not == true){
            document.getElementById("clearButton").style.backgroundColor = "#ff000095";
        }else{
            document.getElementById("clearButton").style.backgroundColor = "#ff000045";
        }
}